package com.aiyige.page.my.message.util;

import com.aiyige.base.eventbus.EventNewMessageChanged;
import com.aiyige.model.message.entity.Message;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.PreferenceUtil;
import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by zhuyakun on 2017/11/28.
 */

public class MessageUtil {
    public static final int MESSAGE_TYPE_LIKE = 1;
    public static final int MESSAGE_TYPE_COLLECTION = 2;
    public static final int MESSAGE_TYPE_COMMENT = 3;
    public static final int MESSAGE_TYPE_ORDER = 4;
    public static final int MESSAGE_TYPE_CONTACT = 5;
    public static final int MESSAGE_TYPE_IM = 6;
    public static final int MESSAGE_TYPE_SYSTEM = 7;
    public static final int MESSAGE_TYPE_INTERACT = 8;
    public static final int MESSAGE_TYPE_ALL = 9;

    public static final int TARGET_TYPE_VIDEO = 1;
    public static final int TARGET_TYPE_PHOTO = 2;
    public static final int TARGET_TYPE_INFO = 3;
    public static final int TARGET_TYPE_SPECIAL_TOPIC = 4;
    public static final int TARGET_TYPE_COMMENT = 5;
    public static final int TARGET_TYPE_OTHER = 6;
    public static final int TARGET_TYPE_LEARN_VIDEO = 7;
    public static final int TARGET_TYPE_MAJOR_COURSE = 8;
    public static final int TARGET_TYPE_PERSONAL_TRAINER = 9;
    public static final int TARGET_TYPE_TRAING_CARD = 10;
    public static final String PREF_KEY_NEW_MESSAGE_NUM_PREFIX = "com.aiyige.pref.message.PREF_KEY_NEW_MESSAGE_NUM";
    public static final String PREF_KEY_NEW_MESSAGE_OBJ_PREFIX = "com.aiyige.pref.message.PREF_KEY_NEW_MESSAGE_OBJ";
    public static final String PREF_KEY_NEW_MESSAGE_TYPE_PREFIX = "com.aiyige.pref.message.PREF_KEY_NEW_MESSAGE_TYPE";


    public static final String PREF_KEY_NEW_MESSAGE_COUNT_PREFIX = "com.aiyige.pref.message.PREF_KEY_NEW_MESSAGE_COUNT";

    public static final int MESSAGE_LEVEL_MAIN = 0;

    public static final int MAX_MESSAGE_LEVEL = 1;
    /**
     * 订单消息状态（与订单状态不是一个东西） 0-待付款，1-已付款，2-已确认，3-申请退款，4-同意退款，5-驳回退款
     */
    public static final int ORDER_STATUS_UNPAY = 0;
    public static final int ORDER_STATUS_PAYED = 1;
    public static final int ORDER_STATUS_CONFIRM = 2;
    public static final int ORDER_STATUS_REFUNDING = 3;
    public static final int ORDER_STATUS_REFUNDED = 4;
    public static final int ORDER_STATUS_REJECT_REFUND = 5;

    public static final int PAGE_TYPE_IM = 1;
    public static final int PAGE_TYPE_CONTACT = 2;

    public static String formatUnReadMessageNum(long num) {
        if (num > 99) {
            return "99+";
        } else {
            return String.valueOf(num);
        }
    }

    public static int convertMomentSubjectToTargetType(String subject) {
        int targetType;
        switch (subject) {
            case Moment.SUBJECT_ARTICLE:
                targetType = MessageUtil.TARGET_TYPE_INFO;
                break;
            case Moment.SUBJECT_VIDEO:
                targetType = MessageUtil.TARGET_TYPE_VIDEO;
                break;
            case Moment.SUBJECT_PHOTO:
                targetType = MessageUtil.TARGET_TYPE_PHOTO;
                break;
            case Moment.SUBJECT_VIDEO_COURSE:
                targetType = MessageUtil.TARGET_TYPE_LEARN_VIDEO;
                break;
            case Moment.SUBJECT_MAJOR_COURSE:
                targetType = MessageUtil.TARGET_TYPE_MAJOR_COURSE;
                break;
            case Moment.SUBJECT_SPECIAL_TOPIC:
                targetType = MessageUtil.TARGET_TYPE_SPECIAL_TOPIC;
                break;
            case Moment.SUBJECT_TRAINING_COURSE:
                targetType = MessageUtil.TARGET_TYPE_TRAING_CARD;
                break;
            case Moment.SUBJECT_PRIVATE_COURSE:
                targetType = MessageUtil.TARGET_TYPE_PERSONAL_TRAINER;
                break;
            default:
                targetType = MessageUtil.TARGET_TYPE_COMMENT;
                break;
        }
        return targetType;
    }


    //    public static void putNewMessage(DataObject dataObject) {
//        for (int i = 0; i < MAX_MESSAGE_LEVEL; ++i) {
//            putNewMessage(i, dataObject);
//        }
//        EventBus.getDefault().post(EventReceiveNewMessage.newBuilder().build());
//    }
//
//    private static void putNewMessage(int level, DataObject dataObject) {
//        if (!checkLevel(level)) return;
//        PreferenceUtil.putLong(PREF_KEY_NEW_MESSAGE_NUM_PREFIX + AccountUtil.getCurrentUser().getId() + level, PreferenceUtil.getLong(PREF_KEY_NEW_MESSAGE_NUM_PREFIX + AccountUtil.getCurrentUser().getId() + level, 0) + 1);
//        PreferenceUtil.putString(PREF_KEY_NEW_MESSAGE_OBJ_PREFIX + AccountUtil.getCurrentUser().getId() + level, JSON.toJSONString(dataObject));
//    }
//
//    public static Pair<Long, DataObject> getNewMessage(int level) {
//        if (!checkLevel(level)) return null;
//        long num = PreferenceUtil.getLong(PREF_KEY_NEW_MESSAGE_NUM_PREFIX + AccountUtil.getCurrentUser().getId() + level, 0);
//        String json = PreferenceUtil.getString(PREF_KEY_NEW_MESSAGE_OBJ_PREFIX + AccountUtil.getCurrentUser().getId() + level, "");
//        DataObject dataObject = null;
//        try {
//            dataObject = JSON.parseObject(json, DataObject.class);
//        } catch (Exception e) {
//
//        }
//        return new Pair<>(num, dataObject);
//    }

    public static int convertServerMessageTypeToAppMessageType(String type) {
        try {
            switch (type) {
                case Message.TYPE_INTERACT:
                case Message.TYPE_COMMENT:
                case Message.TYPE_FAVOURITE:
                case Message.TYPE_PRAISE:
                    return MessageUtil.MESSAGE_TYPE_INTERACT;
                case Message.TYPE_ORDER:
                    return MessageUtil.MESSAGE_TYPE_ORDER;
                case Message.TYPE_SYSTEM:
                    return MessageUtil.MESSAGE_TYPE_SYSTEM;
                default:
                    return MessageUtil.MESSAGE_TYPE_SYSTEM;
            }
        } catch (Exception e) {
            return MessageUtil.MESSAGE_TYPE_SYSTEM;
        }
    }

    public static void receiveNewMessage(int type, int number) {
        if (!AccountUtil.isLogin()) return;
        for (int i = 0; i < MAX_MESSAGE_LEVEL; ++i) {
            receiveNewMessage(i, type, number);
        }
        EventBus.getDefault().post(EventNewMessageChanged.newBuilder().type(type).build());
    }


    public static void consumeNewMessage(int type, int number) {
        if (!AccountUtil.isLogin()) return;
        for (int i = 0; i < MAX_MESSAGE_LEVEL; ++i) {
            consumeNewMessage(i, type, number);
        }
        EventBus.getDefault().post(EventNewMessageChanged.newBuilder().type(type).dontRequestData(true).build());
    }

    public static void updateMessageCount(MessageCount messageCount) {
        if (!AccountUtil.isLogin()) return;
        for (int i = 0; i < MAX_MESSAGE_LEVEL; ++i) {
            updateMessageCount(i, messageCount);
        }
        EventBus.getDefault().post(EventNewMessageChanged.newBuilder().type(MESSAGE_TYPE_ALL).dontRequestData(true).build());
    }

    public static void updateMessageCount(int level, MessageCount messageCount) {
        if (!checkLevel(level)) return;
        PreferenceUtil.putString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, JSON.toJSONString(messageCount));
    }

    private static void receiveNewMessage(int level, int type, int number) {
        if (!checkLevel(level)) return;
        MessageCount messageCount = MessageCount.newBuilder().build();
        try {
            String json = PreferenceUtil.getString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, "");
            MessageCount tempMessageCount = JSON.parseObject(json, MessageCount.class);
            if (tempMessageCount != null) {
                messageCount = tempMessageCount;
            }
        } catch (Exception e) {
        }
        switch (type) {
            case MESSAGE_TYPE_INTERACT:
                messageCount.setInteractCount(messageCount.getInteractCount() + number);
                break;
            case MESSAGE_TYPE_CONTACT:
                messageCount.setContactCount(messageCount.getContactCount() + number);
                break;
            case MESSAGE_TYPE_IM:
                messageCount.setImCount(messageCount.getImCount() + number);
                break;
            case MESSAGE_TYPE_ORDER:
                messageCount.setOrderCount(messageCount.getOrderCount() + number);
                break;
            case MESSAGE_TYPE_SYSTEM:
                messageCount.setSysCount(messageCount.getSysCount() + number);
                break;
        }
        PreferenceUtil.putString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, JSON.toJSONString(messageCount));
    }

    private static void consumeNewMessage(int level, int type, int number) {
        if (!checkLevel(level)) return;
        MessageCount messageCount = MessageCount.newBuilder().build();
        try {
            String json = PreferenceUtil.getString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, "");
            MessageCount tempMessageCount = JSON.parseObject(json, MessageCount.class);
            if (tempMessageCount != null) {
                messageCount = tempMessageCount;
            }
        } catch (Exception e) {
        }
        switch (type) {
            case MESSAGE_TYPE_INTERACT:
                messageCount.setInteractCount(Math.max(messageCount.getInteractCount() - number, 0));
                break;
            case MESSAGE_TYPE_CONTACT:
                messageCount.setContactCount(Math.max(messageCount.getContactCount() - number, 0));
                break;
            case MESSAGE_TYPE_IM:
                messageCount.setImCount(Math.max(messageCount.getImCount() - number, 0));
                break;
            case MESSAGE_TYPE_ORDER:
                messageCount.setOrderCount(Math.max(messageCount.getOrderCount() - number, 0));
                break;
            case MESSAGE_TYPE_SYSTEM:
                messageCount.setSysCount(Math.max(messageCount.getSysCount() - number, 0));
                break;
        }
        PreferenceUtil.putString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, JSON.toJSONString(messageCount));
    }

    public static MessageCount getNewMessageCount(int level) {
        MessageCount messageCount = MessageCount.newBuilder().build();
        if (!checkLevel(level)) return messageCount;
        try {
            String json = PreferenceUtil.getString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, "");
            MessageCount tempMessageCount = JSON.parseObject(json, MessageCount.class);
            if (tempMessageCount != null) {
                messageCount = tempMessageCount;
            }
        } catch (Exception e) {

        }
        return messageCount;
    }

    public static long getTotalNewMessageCount(MessageCount messageCount) {
        return messageCount.getInteractCount() +
                messageCount.getContactCount() +
                messageCount.getImCount() +
                messageCount.getOrderCount() +
                messageCount.getSysCount();
    }

    public static void consumeAllNewMessage(int type) {
        if (!AccountUtil.isLogin()) return;
        for (int i = 0; i < MAX_MESSAGE_LEVEL; ++i) {
            consumeAllNewMessage(i, type);
        }
        EventBus.getDefault().post(EventNewMessageChanged.newBuilder().type(type).dontRequestData(true).build());
    }

    public static void consumeAllNewMessage(int level, int type) {
        if (!checkLevel(level)) return;
        MessageCount messageCount = MessageCount.newBuilder().build();
        try {
            String json = PreferenceUtil.getString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, "");
            MessageCount tempMessageCount = JSON.parseObject(json, MessageCount.class);
            if (tempMessageCount != null) {
                messageCount = tempMessageCount;
            }
        } catch (Exception e) {
        }
        switch (type) {
            case MESSAGE_TYPE_INTERACT:
                messageCount.setInteractCount(0);
                break;
            case MESSAGE_TYPE_CONTACT:
                messageCount.setContactCount(0);
                break;
            case MESSAGE_TYPE_IM:
                messageCount.setImCount(0);
                break;
            case MESSAGE_TYPE_ORDER:
                messageCount.setOrderCount(0);
                break;
            case MESSAGE_TYPE_SYSTEM:
                messageCount.setSysCount(0);
                break;
        }
        PreferenceUtil.putString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, JSON.toJSONString(messageCount));
    }

    public static void clearNewMessage() {
        for (int i = 0; i < MAX_MESSAGE_LEVEL; ++i) {
            clearNewMessage(i);
        }
        EventBus.getDefault().post(EventNewMessageChanged.newBuilder().dontRequestData(true).build());
    }

    public static void clearNewMessage(int level) {
        if (!checkLevel(level)) return;
        PreferenceUtil.putString(PREF_KEY_NEW_MESSAGE_COUNT_PREFIX + AccountUtil.getCurrentUser().getId() + level, "");
    }

    private static boolean checkLevel(int level) {
        return level >= 0 && level < MAX_MESSAGE_LEVEL;
    }


    public static class MessageCount {
        long sysCount;
        long orderCount;
        long contactCount;
        long imCount;
        long interactCount;

        public MessageCount() {
        }

        private MessageCount(Builder builder) {
            setSysCount(builder.sysCount);
            setOrderCount(builder.orderCount);
            setContactCount(builder.contactCount);
            setImCount(builder.imCount);
            setInteractCount(builder.interactCount);
        }

        public static Builder newBuilder() {
            return new Builder();
        }

        public long getSysCount() {
            return sysCount;
        }

        public void setSysCount(long sysCount) {
            this.sysCount = sysCount;
        }

        public long getOrderCount() {
            return orderCount;
        }

        public void setOrderCount(long orderCount) {
            this.orderCount = orderCount;
        }

        public long getContactCount() {
            return contactCount;
        }

        public void setContactCount(long contactCount) {
            this.contactCount = contactCount;
        }

        public long getImCount() {
            return imCount;
        }

        public void setImCount(long imCount) {
            this.imCount = imCount;
        }

        public long getInteractCount() {
            return interactCount;
        }

        public void setInteractCount(long interactCount) {
            this.interactCount = interactCount;
        }

        public static final class Builder {
            private long sysCount;
            private long orderCount;
            private long contactCount;
            private long imCount;
            private long interactCount;

            private Builder() {
                sysCount = 0;
                orderCount = 0;
                contactCount = 0;
                imCount = 0;
                interactCount = 0;
            }

            public Builder sysCount(long val) {
                sysCount = val;
                return this;
            }

            public Builder orderCount(long val) {
                orderCount = val;
                return this;
            }

            public Builder contactCount(long val) {
                contactCount = val;
                return this;
            }

            public Builder imCount(long val) {
                imCount = val;
                return this;
            }

            public Builder interactCount(long val) {
                interactCount = val;
                return this;
            }

            public MessageCount build() {
                return new MessageCount(this);
            }
        }
    }

}
